#include "AVDeMuxer.h"
#include<QDebug>
extern"C"
{
#include<libavformat/avformat.h>
}

using AVPackePtr = std::shared_ptr<AVPacket>;
using AVFramePtr = std::shared_ptr<AVFrame>;

AVDeMuxer::AVDeMuxer()
   :quit_(false)

{


    pFormatCtx_ = avformat_alloc_context();
    //设置参数
    pFormatCtx_->max_ts_probe=50;
    pFormatCtx_->interrupt_callback.callback = INterruptFOuntion;
    pFormatCtx_->interrupt_callback.opaque = this;
    pFormatCtx_->flags |= AVFMT_FLAG_DISCARD_CORRUPT;


}

bool AVDeMuxer::Open(const QString path)
{
    //rtsp rtmp hls webrtc rtmp://ip:port/live/streamname

    readThread_.reset(new std::thread([this,path](){
        this->FetchStream(path);
    }));
    return true;
}

void AVDeMuxer::Close()
{
    quit_ = true;
    //将标志位置位
    if(readThread_)
    {
        readThread_.reset();
        readThread_ = nullptr;
    }
    if(pFormatCtx_)
    {
        avformat_free_context(pFormatCtx_);
    }

}

void AVDeMuxer::FetchStream(const QString path)
{
    bool ret = true;
    if(streamCb_)
    {
        ret = FetchStreamInfo(path);
        streamCb_(ret);
    }
    if(!ret)
    {
        return;
    }
    AVPackePtr packet = nullptr;
    while(!quit_ && pFormatCtx_){
        packet = AVPackePtr(av_packet_alloc(),[](AVPacket* p){av_packet_free(&p);});
        int re = av_read_frame(pFormatCtx_,packet.get());
        if(re == 0)
        {
            if(packet->stream_index == videoIndex)
            {
                //视频包数据
                //开始解码音视频数据包
                qDebug()<<"视频包";
            }
            else if(packet->stream_index == AudioIndex)
            {
                //音频数据包
                qDebug()<<"音频包";

            }
        }
        else
        {
            av_packet_unref(packet.get());
            break;
        }
    }

}

bool AVDeMuxer::FetchStreamInfo(const QString path)
{
    //获取流信息
    if(avformat_open_input(&pFormatCtx_,path.toStdString().c_str(),nullptr,nullptr)!=0)
    {
        qDebug()<< "avformate_open_input failed";
        return false;
    }
    if(avformat_find_stream_info(pFormatCtx_,nullptr)!=0)
    {
        qDebug()<<"avformate_find_stream_info failed";
        return false;
    }

    for(int i = 0;i < pFormatCtx_->nb_streams;i++)
    {
        if(pFormatCtx_->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
        {
            videoIndex = i;
        }
        else if(pFormatCtx_->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
        {
            AudioIndex = i;
        }
        else
        {
            continue;
        }
    }
    if(videoIndex != -1)
    {
        qDebug()<<"含有视频流";
    }
    if(AudioIndex != -1)
    {
        qDebug()<<"含有音频流";
    }
    return true;

}

int AVDeMuxer::INterruptFOuntion(void *arg)
{
    AVDeMuxer* thiz = (AVDeMuxer*)arg;
    return thiz->quit_;

}
